//
//  ViewController.m
//  shadow
//
//  Created by Pro on 2022/1/13.
//

#import "ViewController.h"

#import "YZInfoColorBgCell.h"

#import "CustomView.h"

#import "YZChartView.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView * view1;
@property(nonatomic,strong)UIView * view2;

@property(nonatomic,strong)CALayer * layer1;
@property(nonatomic,strong)CALayer * layer2;

@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * infoArray;
@property(nonatomic,strong)YZChartView * yzChartView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    // Do any additional setup after loading the view.
//    self.view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//    self.view1.layer.contents = (__bridge id)[UIImage imageNamed:@"2.jpg"].CGImage;
//    self.view1.layer.contentsGravity = kCAGravityResizeAspect;
////    self.view1.layer.masksToBounds = YES;
//    self.view1.backgroundColor = [UIColor redColor];
//    [self.view addSubview:self.view1];
//
//
//    self.view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 260, 100, 100)];
//    self.view2.layer.contents = (__bridge id)[UIImage imageNamed:@"1.png"].CGImage;
//    self.view2.layer.contentsGravity = kCAGravityResizeAspect;
////    self.view2.backgroundColor = [UIColor blueColor];
////    self.view2.layer.masksToBounds = YES;
//    [self.view addSubview:self.view2];
//
//    self.view1.layer.shadowOpacity = 0.5f;
//      self.view2.layer.shadowOpacity = 0.5f;
//      //create a square shadow
//      CGMutablePathRef squarePath = CGPathCreateMutable();
//     CGPathAddRect(squarePath, NULL, CGRectMake(-20, -20, 140, 140));
//
//    CGMutablePathRef circlePath = CGPathCreateMutable();
//    self.view1.layer.shadowPath = squarePath;
//    CGPathRelease(squarePath);
//
//    CGPathAddEllipseInRect(circlePath, NULL, CGRectMake(-20, -20, 140, 140));
//    self.view2.layer.shadowPath = circlePath;
//    CGPathRelease(circlePath);
    
//    CustomView * vier = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, 414, 500)];
//    [self.view addSubview:vier];
    
//根据上图的实例，实现以上两个功能
    
    self.layer1 = [[CALayer alloc] init];
    self.layer1.frame = CGRectMake(100, 100, 200, 200);
    self.layer1.contents = (__bridge id)[UIImage imageNamed:@"3.jpg"].CGImage;
    self.layer1.contentsGravity = kCAGravityResizeAspect;
    [self.view.layer addSublayer:self.layer1];

    self.layer2 = [[CALayer alloc] init];
    self.layer2.frame = CGRectMake(100, 240, 200, 200);
    self.layer2.contents = (__bridge id)[UIImage imageNamed:@"4.jpg"].CGImage;
    self.layer2.contentsGravity = kCAGravityResizeAspect;
    [self.view.layer addSublayer:self.layer2];
    
    
    
    NSError *error;
    NSString *_ljPath = [[NSBundle mainBundle]pathForResource:@"info" ofType:@"json"];
    NSDictionary *_ljDic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:_ljPath] options:NSJSONReadingMutableLeaves error:&error];
//    NSLog(@"%@",_ljDic);
    self.infoArray = _ljDic[@"data"];
    [self.view addSubview:self.tableView];
    
    NSLog(@"%@",NSStringFromCGRect(self.view.frame));
    
    NSString * _yzpath = [[NSBundle mainBundle] pathForResource:@"line" ofType:@"json"];
    NSDictionary * _yzDic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:_yzpath] options:NSJSONReadingMutableLeaves error:&error];
    NSLog(@"%@",_yzDic);
    
    [self.view addSubview:self.yzChartView];
    [self.yzChartView setDic:_yzDic];
}

-(UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 350)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.allowsSelection = NO;
        _tableView.userInteractionEnabled = NO;
    }
    return _tableView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YZInfoColorBgCell * cell = [YZInfoColorBgCell cellWithTableView:tableView];
    NSDictionary * dic = [self.infoArray objectAtIndex:indexPath.row];
    [cell setDictionary:dic];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 60)];
    infoView.backgroundColor = [UIColor systemGroupedBackgroundColor];
    UILabel * num  = [[UILabel alloc] initWithFrame:CGRectMake(5, 15, 100, 30)];
    num.text = @"数量(CUP)";
    num.textColor = [UIColor blackColor];
    num.textAlignment = NSTextAlignmentLeft;
    num.font = [UIFont systemFontOfSize:17];
    [infoView addSubview:num];
    
    
    UILabel * centerLab = [[UILabel alloc] initWithFrame:CGRectMake(414 / 2 - 100 / 2,15, 100, 30)];
    centerLab.text = @"价格(YP)";
    centerLab.textColor = [UIColor blackColor];
    centerLab.textAlignment = NSTextAlignmentCenter;
    centerLab.font = [UIFont systemFontOfSize:17];
    [infoView addSubview:centerLab];
    
    UILabel * rightLab = [[UILabel alloc] initWithFrame:CGRectMake(414 - 100,15, 100, 30)];
    rightLab.text = @"数量(CUP)";
    rightLab.textColor = [UIColor blackColor];
    rightLab.textAlignment = NSTextAlignmentCenter;
    rightLab.font = [UIFont systemFontOfSize:17];
    [infoView addSubview:rightLab];
    return  infoView;
}

-(YZChartView *)yzChartView {
    if (_yzChartView == nil) {
        _yzChartView = [[YZChartView alloc] initWithFrame:CGRectMake(0, 500, [UIScreen mainScreen].bounds.size.width, 240)];
    }
    return _yzChartView;
}

@end

